package controllers

import (
	"github.com/revel/revel"
	"waterblabla/app/models"
)

type Provider struct {
	Controller
}

func (c Provider) GetProviders() revel.Result {
	var qs models.QS
	if err := models.ParseBodyAndValidate(c.body, &qs); err != nil {
		return c.RenderResult(err)
	}

	var providers []models.Provider
	if err := models.GetProvidersBriefly(&providers); err != nil {
		return c.RenderResult(err)
	}
	return c.RenderResult(providers)
}

func (c Provider) AddProvider() revel.Result {
	var provider models.Provider
	if err := models.ParseBodyAndValidate(c.body, &provider); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(models.AddProvider(&provider))
}

func (c Provider) RmProvider() revel.Result {
	type Req struct {
		ProviderID string   `json:"providerID" valid:"Required"`
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(models.RmProvider(req.ProviderID))
}

func (c Provider) UpdateProvider() revel.Result {
	var provider models.Provider
	if err := models.ParseBodyAndValidate(c.body, &provider); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(models.UpdateProvider(&provider))
}
